#!/usr/bin/env ruby

ENV["RAILS_ENV"] ||= "production"
require_relative "../config/environment"

name, number, email = ARGV

abort "Usage: #{$PROGRAM_NAME} [GEM_NAME=hola] [VERSION=all] [AS=email]" if [name, number, email].any?(&:blank?)

rubygem = Rubygem.find_by_name!(name)

versions = if number == "all"
             rubygem.versions
           else
             [rubygem.versions.find_by_number!(number)]
           end

owner = User.find_by_email!(email)

puts "Permanently delete #{name} with #{versions.size} versions? (y/N)"
confirm = $stdin.gets.chomp

if /^y(es)?$/i.match?(confirm)
  versions.each do |version|
    owner.deletions.create!(version: version)
  end

  puts "#{name} permadeleted."
else
  abort "No problem! #{name} lives on."
end
